package com.lindroy.keyboarddemo.util.keyboard

import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.lindroy.keyboarddemo.R
import com.lindroy.keyboarddemo.util.screenHeight
import com.lindroy.keyboarddemo.util.statusBarHeight

/**
 * @author Lin
 * @date 2021/12/23
 * @function https://www.jianshu.com/p/bfdffc88bf0a
 */
/**
 * 获取键盘高度帮助类
 *
 * @author Liuzj
 * @date 2021/3/5
 */
class KeyboardHeightProvider(
    activity: FragmentActivity,
    listener: KeyboardHeightListener?,
    ignoreStatusBarHeight: Boolean = false
) : PopupWindow(activity) {

    private val parentView by lazy { activity.window.decorView.rootView }

    interface KeyboardHeightListener {
        fun onKeyboardHeightChanged(
            keyboardHeight: Int,
            keyboardOpen: Boolean,
            isLandscape: Boolean
        )
    }

    init {
        val popupView = FrameLayout(activity)
        popupView.setBackgroundColor(ContextCompat.getColor(activity,R.color.bg))
        popupView.layoutParams =
            LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        popupView.viewTreeObserver.addOnGlobalLayoutListener {
            val metrics = DisplayMetrics()
            activity.windowManager.defaultDisplay.getMetrics(metrics)
            val rect = Rect()
            popupView.getWindowVisibleDisplayFrame(rect)
            var keyboardHeight: Int = metrics.heightPixels - (rect.bottom - rect.top)
            val resourceID: Int =
                activity.resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceID > 0 && !ignoreStatusBarHeight) {
//                keyboardHeight -= activity.resources.getDimensionPixelSize(resourceID)
            }
            if (keyboardHeight < 100) {
                keyboardHeight = 0
            }
            //是否是横屏
            val isLandscape = metrics.widthPixels > metrics.heightPixels
            val keyboardOpen = keyboardHeight > 0
            listener?.onKeyboardHeightChanged(keyboardHeight, keyboardOpen, isLandscape)
        }
        contentView = popupView
        softInputMode =
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
        inputMethodMode = INPUT_METHOD_NEEDED
        width = 200
        height = ViewGroup.LayoutParams.MATCH_PARENT
        setBackgroundDrawable(ColorDrawable(0))
        parentView.post { showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0) }

        //activity 销毁时必须关闭 popupWindow ，避免内存泄漏
        activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                dismiss()
            }
        })
    }
}
